import { ChangeDetectorRef, Component, Injector, OnInit, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { UrlHelper } from '@shared/helpers/UrlHelper';
import { ChatSignalrService } from 'app/shared/layout/chat/chat-signalr.service';
import { AppComponentBase } from 'shared/common/app-component-base';
import { SignalRHelper } from 'shared/helpers/SignalRHelper';
import { LinkedAccountsModalComponent } from '@app/shared/layout/linked-accounts-modal.component';
import { UserDelegationsModalComponent } from '@app/shared/layout/user-delegations-modal.component';
import { ChangePasswordModalComponent } from '@app/shared/layout/profile/change-password-modal.component';
import { ChangeProfilePictureModalComponent } from '@app/shared/layout/profile/change-profile-picture-modal.component';
import { MySettingsModalComponent } from '@app/shared/layout/profile/my-settings-modal.component';
import { NotificationSettingsModalComponent } from '@app/shared/layout/notifications/notification-settings-modal.component';
import { UserNotificationHelper } from '@app/shared/layout/notifications/UserNotificationHelper';
import { DateTime } from 'luxon';
import { DateTimeService } from './shared/common/timing/date-time.service';
import { FileFinderModalComponent } from '@app/shared/common/file-finder/file-finder-modal.component';
import { SessionServiceProxy } from '@shared/service-proxies/service-proxies';
import { AppBsModalService } from '@shared/common/appBsModal/app-bs-modal.service';

@Component({
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.less']
})
export class AppComponent extends AppComponentBase implements OnInit {

    theme: string;
    installationMode = true;

    @ViewChild('linkedAccountsModal') linkedAccountsModal: LinkedAccountsModalComponent;
    @ViewChild('userDelegationsModal', {static: true}) userDelegationsModal: UserDelegationsModalComponent;
    @ViewChild('changePasswordModal', {static: true}) changePasswordModal: ChangePasswordModalComponent;
    @ViewChild('changeProfilePictureModal', {static: true}) changeProfilePictureModal: ChangeProfilePictureModalComponent;
    @ViewChild('mySettingsModal', {static: true}) mySettingsModal: MySettingsModalComponent;
    @ViewChild('notificationSettingsModal', {static: true}) notificationSettingsModal: NotificationSettingsModalComponent;
    @ViewChild('chatBarComponent') chatBarComponent;

    IsSessionTimeOutEnabled: boolean = this.setting.getBoolean('App.UserManagement.SessionTimeOut.IsEnabled') && this.appSession.userId != null;

    listImageData: string[] = [];
    displayCustom = false;
    responsiveOptions: any[] = [
        {
            breakpoint: '1500px',
            numVisible: 7
        },
        {
            breakpoint: '1024px',
            numVisible: 5
        },
        {
            breakpoint: '1024px',
            numVisible: 5
        },
        {
            breakpoint: '768px',
            numVisible: 3
        },
        {
            breakpoint: '560px',
            numVisible: 1
        }
    ];


    public constructor(
        injector: Injector,
        private _chatSignalrService: ChatSignalrService,
        private _userNotificationHelper: UserNotificationHelper,
        private _dateTimeService: DateTimeService,
        private _modalService: AppBsModalService,
        private _sessionService: SessionServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(): void {
        this._userNotificationHelper.settingsModal = this.notificationSettingsModal;
        this.theme = abp.setting.get('App.UiManagement.Theme').toLocaleLowerCase();
        this.installationMode = UrlHelper.isInstallUrl(location.href);

        this.registerModalOpenEvents();

        if (this.appSession.application) {
            //SignalRHelper.initSignalR(() => { this._chatSignalrService.init(); });
        }

        setTimeout(()=> this.ping(),600000);
    }

    registerModalOpenEvents(): void {
        abp.event.on(AppConsts.EventName.appShowLinkedAccountsModal, () => {
            setTimeout(() => {
                this.linkedAccountsModal.show();
            }, 300);
        });

        abp.event.on(AppConsts.EventName.appShowUserDelegationsModal, () => {
            setTimeout(() => {
                this.userDelegationsModal.show();
            }, 300);
        });

        abp.event.on(AppConsts.EventName.appShowChangePasswordModal, () => {
            setTimeout(() => {
                this.changePasswordModal.show();
            }, 300);
        });

        abp.event.on(AppConsts.EventName.appShowChangeProfilePictureModal, () => {
            setTimeout(() => {
                this.changeProfilePictureModal.show();
            }, 300);
        });

        abp.event.on(AppConsts.EventName.appShowMySettingsModal, () => {
            setTimeout(() => {
                this.mySettingsModal.show();
            }, 300);
        });

        abp.event.on('abp.ckeditor.filefinder', (data) => {
            const bsModalRef = this._modalService.show(FileFinderModalComponent, {
                backdrop: 'static', class: 'modal-xl'
            });
    
            bsModalRef.content.configure(data);
            bsModalRef.content.shown();
        });
        abp.event.on(AppConsts.EventName.showImageView, (data) => {
            this.listImageData = [data];
            this.displayCustom = true;
        });
    }

    getRecentlyLinkedUsers(): void {
        abp.event.trigger('app.getRecentlyLinkedUsers');
    }

    onMySettingsModalSaved(): void {
        abp.event.trigger('app.onMySettingsModalSaved');
    }

    ping():void{
        this._sessionService.ping().subscribe(r=>{
            setTimeout(()=>this.ping(),600000);
        });
    }
}
